package mainProcess;

// class MainProcessing
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.lo23.network.*;
import ihm.main.Traitement;

import com.lo23.data.objgroup.*;
import com.lo23.data.objinterface.*;
import com.lo23.data.objpicture.*;
import com.lo23.data.utils.*;
import com.lo23.data.xml.*;

public class MainProcessing {
	// private int UID;
	// private String username;
	private UserAction userAction;
	private ProtocolListener ProtocolListener;
	private NetworkManager networkManager;
	private List<User> listOnlineUsers;
	private DataInterface dataInterface;
	private TraitementData traitementData;

	// private List<UserProfile> listFriendsProfile;

	// public methodes
	public MainProcessing(/* InterfaceIHM *IHM::Main::Traitement */) {
		this.userAction = new UserAction();
		this.traitementData = new TraitementData();
		this.ProtocolListener = new ProtocolListener(this);
		this.listOnlineUsers = new ArrayList<User>();
		// this.listFriendsProfile = new ArrayList();

		// TO DO: faire les setMainProcessing des classes qui en ont besoin
		userAction.setMainProcessing(this);

		this.dataInterface = new DataInterface();
		userAction.setDataInterface(dataInterface);
		traitementData.setDataInterface(dataInterface);
	}

	public String getUsername() {
		LocalUser lUser = this.dataInterface.getLocalUser(null);
		return lUser.getLogin();
	}

	public DataInterface getDataInterface() {
		return this.dataInterface;
	}

	public void setListOnlineUsers(List<User> users) {
		this.listOnlineUsers = users;
	}

	public List<User> getListOnlineUsers() {
		return this.listOnlineUsers;
	}

	public void addOnlineUser(User newUser) {
		this.listOnlineUsers.add(newUser);
		Traitement.getInstance().userConnection(newUser);
	}

	public User getUserWithUID(String uid) {
		for (User user : this.listOnlineUsers)
			if (user.getuID().equals(uid))
				return user;
		return null;
	}

	public NetworkManager getNetworkManager() {
		return this.networkManager;
	}

	public void removeOnlineUser(User user) {
		int index = this.listOnlineUsers.indexOf(user);
		this.listOnlineUsers.remove(index);
		Traitement.getInstance().userDisconnection(user);
	}

	public TraitementData getTraitementData() {
		return this.traitementData;
	}

	public ProtocolListener getProtocolListener() {
		return this.ProtocolListener;
	}

	public void setNetworkManager(NetworkManager n) {
		this.networkManager = n;
	}

	public UserAction getUserAction() {
		return this.userAction;
	}

	public void close() {
		if (this.networkManager == null)
			return;

		networkManager.close();
	}

	/*
	 * public UserProfile getFriendProfile(String friendID) { Iterator itr =
	 * this.listFriendsProfile.iterator(); while (itr.hasNext()) { UserProfile p
	 * = itr.next(); if (p.getUID() == friendID) { return p; } } }
	 */

	/*
	 * public void addFriendProfile(UserProfile p) {
	 * this.listFriendsProfile.add(p); }
	 */

}
